// pages/demo/demo.js
// 词汇
import { net2 } from '../../api/net2.js'
let app = getApp()
let CODE_SRC = app.globalData.CODE_SRC
const db = wx.cloud.database({env: app.globalData.CLOUD_ENV})
const DBBanner = db.collection('db_banner')
let t = null;
// console.log(word.lists);
// 创建音频的上下文对象
const innerAudioContext = wx.createInnerAudioContext({
  useWebAudioImplement: true
})

Page({

  /**
   * 页面的初始数据
   */
  data: {
      isShow: false,
      all: net2,
      lists: [],
      arr: [1,2,3,4,5],
      total: 0,
      count: 0,
      num: 0,
      title: '每天积累一个前端编程常见词汇。',
      message: '关注微信公众号,学习更多编码知识',
      i: -1,
      sqaure: 0,
      CODE_SRC
  },

  setIndexFn(e){
    let sqaure = e.currentTarget.dataset.index;
    console.log(e)
    if(net2[sqaure].arr.length != 0){
      this.setData({
        lists:net2[sqaure].arr,
        total: net2[sqaure].arr.length,
        sqaure:sqaure,
        isShow: false
      })
      this.initFn();
    }
  },

  addClick(e){
    let page = e.currentTarget.dataset.page;
    let num = page - 0;
    // console.log({page})
    this.initFn(num);
  },
  // 初始化
  initFn(page, pageSize){
    var page = page || 0;
    var pageSize= pageSize || 15;
    let start = page * pageSize;
    let end = start + pageSize;
    let arr = this.data.lists.slice(start,end)
    let count = Math.ceil(this.data.total / pageSize )
    this.setData({
        arr,
        count,
        num: page,
        i: -1
    })
  },

  loopFn(){
    let _title = this.data.title;
    let _f = _title.slice(0,1)
    let _e = _title.slice(1)
    let title = _e+_f;
    this.setData({title})
  },

  async getMessageFn(){
    let that = this;
    await DBBanner.get().then(
      rows=>{
        // console.log(rows)
        let {errMsg,data} = rows;
        if(errMsg == "collection.get:ok") {
          let {title,message} = data[0];
          wx.setStorageSync('_title', title)
          wx.setStorageSync('_message', message)
          wx.setStorageSync('_start', new Date().getTime())
          that.setData({
            title,
            message})
        }
       
      }
    )
    .catch(
      err=>{
         console.log(err)
      }
    )
  },

  // 获取音频文件
  getAudioFn(e){
    let word = e.currentTarget.dataset.word;
    let index = e.currentTarget.dataset.index;
    if(word){
      let src = `https://dict.youdao.com/dictvoice?audio=${word}&type=2`;
      innerAudioContext.src = src
      innerAudioContext.play()
    }
    // if(index){
      this.setData({
        i: index
      })
    // }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      if(net2[0].arr.length != 0){
        this.setData({
          lists:net2[0].arr,
          total: net2[0].arr.length
        })
        this.initFn();
      }
      let title = wx.getStorageSync('_title')
      let message = wx.getStorageSync('_message')
      let _start = wx.getStorageSync('_start')
      if(title){
         let _end = new Date().getTime()
         let times = 1 * 24 * 60 * 60 * 1000
         if((_end - _start) > times) {
            this.getMessageFn();
         }
         else {
            this.setData({title,message})
         }
      }
      else {
          this.getMessageFn();
      }
  },
  showContentFn(){
      this.setData({isShow: true})
  },
  hideContentFn(){
    this.setData({isShow: false})
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    if(t!=null) clearInterval(t);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})